import re
from datetime import datetime


def extract_datetime(txt):
    regex_patterns = [
        r"(\d{4})年(\d{1,2})月(\d{1,2})日\s*(\d{1,2}:\d{2}(?::\d{2})?)?",
        r"(\d{4})-(\d{1,2})-(\d{1,2})\s*(\d{1,2}:\d{2}(?::\d{2})?)?",
        r"(\d{4})/(\d{1,2})/(\d{1,2})\s*(\d{1,2}:\d{2}(?::\d{2})?)?",
        r"(\d{4}).(\d{1,2}).(\d{1,2})\s*(\d{1,2}:\d{2}(?::\d{2})?)?",
        r"-(\d{4})-\s+(\d{1,2})/(\d{1,2})\s+(\d{2}:\d{2})?",
    ]
    for pat in regex_patterns:
        try:
            re_output = list(re.search(pat, txt).groups())
            if re_output:
                year, month, day, t = re_output
                if t is None:
                    h, m, s = (0, 0, 0)
                elif len(t) <= 5 and ":" in t:
                    h, m = t.split(":")
                    s = 0
                elif ":" in t:
                    h, m, s = t.split(":")
                return datetime(int(year), int(month), int(day), int(h), int(m), int(s))
        except:
            pass
    return None


def beautify(txt: str, negative_keywords=[]):
    txt = re.sub(r'(\n\s*)+', '\n', txt)
    for keyword in negative_keywords:
        txt = re.sub(keyword, '', txt)
    txt = txt.strip("\n ")
    return txt